/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upc.clase.demo.dao.impl;

import edu.upc.clase.demo.dao.AdministradorDao;
import edu.upc.clase.demo.entity.Administrador;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author HP
 */

@Repository
public class AdministradorDaoImpl extends SimpleJdbcDaoSupport implements AdministradorDao{
    
    private static Logger log = LoggerFactory.getLogger(AdministradorDaoImpl.class);
    
    @Autowired
    public AdministradorDaoImpl(DataSource dataSource) {
        log.info("Asignando el dataSource");
        setDataSource(dataSource);
    }

    @Override
    public Integer insertar(Administrador administrador) {
        getJdbcTemplate().update(
                "insert into administrador (cargo,contrasena,superAdm, resto, carta, orden, admin) values (?, ?, ?, ?, ?, ?, ?)",
                 administrador.getContrasena(), administrador.getCargo(), administrador.isSuperad(), administrador.isMmtResto(), administrador.isMmtCarta(), administrador.isMmtOrden(), administrador.isMmtAdmin());
        return getSimpleJdbcTemplate().queryForInt("select last_insert_id()");
    }

    @Override
    public void actualizar(Administrador administrador) {
        getJdbcTemplate().update(
                "update administradores set cargo = ?, contrasena = ?",
                administrador.getCargo(), administrador.getContrasena());
    }

    @Override
    public void eliminar(Administrador administrador) {
        getJdbcTemplate().update(
                "delete from administradores where id = ?");
    }
    
}
